import {
  mpId
} from "../../utils/config";
import {
  getWxACodeUnLimitPath
} from "../../api/miniProgramController";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    posterImage: '',
    custId: '',
    userInfo: '',

    canvasHidden: false,
    rpx: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let self = this
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          rpx: res.windowWidth / 320
        })
      }
    })

    self.setData({
      custId: wx.getStorageSync('custId'),
      userInfo: wx.getStorageSync('userBase')
    })

    if(this.data.userInfo.cloudTicket>0){
      self.getWxACodeUnLimitPath()
    }
  },
  /**
   * 获取base64
   */
  async getWxACodeUnLimitPath() {
    let custId = wx.getStorageSync('custId') || 1
    let resolve = await getWxACodeUnLimitPath({
      custId: custId,
      scene: `bbId=${custId}`,
      mpId: mpId,
      page: 'pages/index/index/index',
      width: 430,
    })
    if (resolve.success) {
      console.log("邀请码路径："+resolve.data)
      this.setData({
        posterImage: resolve.data
      })
    }
  },
  /**
   * 生成海报
   */
  drawImg(e) {
    let self = this
    wx.showLoading({
      title: '生成海报中···',
    })

    let rpx = self.data.rpx
    const ctx = wx.createCanvasContext('myCanvas')
    ctx.drawImage('../../static/images/poster.png', 0, 0, 320 * rpx, 508 * rpx)

    ctx.setFontSize(15);
    ctx.setTextAlign('center')
    ctx.fillText(self.data.userInfo.nickname + '的邀请码', 160 * rpx, 200 * rpx)

    ctx.drawImage('../../static/images/poster-a.png', 0, 340 * rpx, 340 * rpx, 80 * rpx)
    ctx.drawImage('../../static/images/poster-b.png', 0, 400 * rpx, 340 * rpx, 80 * rpx)

    wx.getImageInfo({
      src: self.data.posterImage,
      success: function (res) {
        ctx.drawImage(res.path, 100 * rpx, 210 * rpx, 120 * rpx, 120 * rpx)
        ctx.draw(false, function (e) {
          self.savePoster()
        })
      },
      fail: function (err) {
        console.log(err)
        wx.showToast({
          title: '网络错误请重试',
          icon: 'loading'
        })
      }
    })
  },
  /**
   * 保存图片到系统相册。
   */
  savePoster() {
    wx.canvasToTempFilePath({
      canvasId: 'myCanvas',
      success: function (res) {
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
            });
          },
          fail() {
            wx.showToast({
              title: '保存失败',
              icon: 'error'
            });
            wx.hideLoading()
          }
        })
      }
    })
  },
  /**
   * 保存图片到系统相册。
   */
  savePoster2() {
    wx.canvasToTempFilePath({
      canvasId: 'myCanvas',
      success: function (res) {
        console.log(res);
        console.log('0000000000000000');
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePath, //图片路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success(res) {
                wx.hideLoading();
                wx.showToast({
                  title: '保存成功',
                });
              },
              fail() {
                wx.hideLoading()
              }
            })
          }
        })
      }
    })
  },
})